//
//  BaiduLoaction.m
//  honglaba
//
//  Created by 红喇叭 on 2017/6/14.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "BaiduLoaction.h"

@implementation BaiduLoaction
- (void)Start:(YYLoctionSuccess)success loctionFail:(YYLoctionFail)fail{
    self.success = success;
    self.fail = fail;
    [self startLoction];
   // [self getCityInfo];
}

- (void)startLoction{
    _geocodesearch = [[BMKGeoCodeSearch alloc] init];
    
    _geocodesearch.delegate = self;
    
    //初始化BMKLocationService
    
    _localService = [[BMKLocationService alloc]init];
    
    _localService.delegate = self;
    
    _localService.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    
    //启动LocationService
    
    [_localService startUserLocationService];
}

- (void)stopLoction{
    [_localService stopUserLocationService];
    _geocodesearch = nil;
    _geocodesearch.delegate = nil;
    _localService = nil;
    _localService.delegate = nil;
}

//- (void)getCityInfo{
//    BMKDistrictSearch *search = [[BMKDistrictSearch alloc] init];
//    BMKDistrictSearchOption *option = [[BMKDistrictSearchOption alloc] init];
//    option.city = @"重庆市";
//    [search districtSearch:option];
//    search.delegate = self;
//}
//
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation

{
    
    NSLog(@"heading is %@",userLocation.heading);
    
}

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
    
    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
    
    reverseGeocodeSearchOption.reverseGeoPoint = userLocation.location.coordinate;
    
    BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
    
    if(flag){
        
        NSLog(@"反geo检索发送成功");
        
        [_localService stopUserLocationService];
        
    }else{
        
        self.fail(@"地理反检索失败");
        NSLog(@"反geo检索发送失败");
        
    }
    

}


#pragma mark -------------地理反编码的delegate---------------

-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error

{
    
    NSLog(@"address:%@----%@",result.addressDetail,result.address);
    self.success(result);
    //反编码成功之后停止定位 否则 位置变化时会更新位置信息
    [self stopLoction];
    //addressDetail:     层次化地址信息
    
    //address:    地址名称
    
    //businessCircle:  商圈名称
    
    // location:  地址坐标
    
    //  poiList:   地址周边POI信息，成员类型为BMKPoiInfo
    
}

- (void)didFailToLocateUserWithError:(NSError *)error{
        
        NSLog(@"error:%@",error);
    self.fail(@"定位失败");
        
}
//
//
//- (void)onGetDistrictResult:(BMKDistrictSearch *)searcher result:(BMKDistrictResult *)result errorCode:(BMKSearchErrorCode)error{
//    
//}

@end
